package com.ciquan.server.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by kakalee on 14-10-14.
 */
public class DateUtils {

    private static Logger logger = Logger.getLogger(DateUtils.class.getName());

    public final static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public final static String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public final static String yyyyMMdd = "yyyy-MM-dd";
    public final static String yyyyMM = "yyyy-MM";

    public static String formatToString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static String formatToString(long date, String format) {
        Date d = new Date(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(d);
    }

    public static Date parse(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            logger.error(e);
        }

        return new Date();
    }

    public static String today() {
        return formatToString(new Date(), yyyyMMdd);
    }

    public static String miniute(int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, amount);
        return formatToString(calendar.getTime(), yyyyMMddHHmm);
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);// Sunday
        return c.getTime();
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);// Sunday
        return c.getTime();
    }

    public static Date getFirstDayOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, 1);
        return c.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return c.getTime();
    }

    public static Date getFirstDayOfLastMonth(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);// Sunday
        return c.getTime();
    }

    public static Date getLastDayOfLastMonth(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return c.getTime();
    }

    public static void main(String args[]) {
        System.out.println(formatToString(getLastDayOfLastMonth(new Date()), yyyyMMdd));
    }
}
